import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import '../../common/colors.dart';
import '../home_module/home_model.dart';
import 'all_secruty_controller.dart';

class AllSecrutyView extends GetView {
  AllSecrutyView({super.key});

  AllSecrutyController controller = Get.put(AllSecrutyController());

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Colors.grey.shade200,
      appBar: AppBar(
        leading: IconButton(
          onPressed: () {
            Get.back(result: 'refresh');
          },
          icon: Image.asset(
            'images/leftarr.png',
            width: 15,
            height: 15,
          ),
        ),
        title: const Text(
          '全部',
          style: TextStyle(color: Colors.black, fontSize: 16),
        ),
        actions: [
          // TextButton(onPressed: (){
          //   _getStorageData();
          // }, child: Text('11'))
        ],
      ),
      body: EasyRefresh.custom(
        slivers: [
          GetBuilder<AllSecrutyController>(builder: (_) {
            return SliverList(
              delegate: _mySliverChildBuilderDelegate(),
            );
          })
        ],
      ),
    );
  }

  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
      (BuildContext context, int index) {
        return Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                  margin: EdgeInsets.only(left: 5, top: 10, bottom: 10),
                  color: Colors.grey.shade200,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        '${controller.model?.data?[index].categoryName}',
                        style: TextStyle(
                            color: AppColor.smallTextColor, fontSize: 13),
                      ),
                    ],
                  )),
              ReorderableListView.builder(
                  shrinkWrap: true,
                  itemBuilder: (BuildContext context, int a) {
                    AppGridModel appGridModel = controller.dataArr[index];
                    Apps appModel = appGridModel.data![index].apps![a];
                    return Container(
                      color: Colors.white,
                      // padding: EdgeInsets.all(10),
                      padding: EdgeInsets.only(
                        left: 15,
                      ),
                      key: ValueKey(a), //必须要有唯一的 不重复的key
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Row(
                                children: [
                                  Image.asset(
                                    'images/drag_icon.png',
                                    width: 10,
                                    height: 10,
                                  ),
                                  SizedBox(
                                    width: 5,
                                  ),
                                  Text(
                                    '${appModel.name}',
                                    style: TextStyle(fontSize: 15),
                                  ),
                                ],
                              ),
                              Container(
                                width: 50,
                                height: 40,
                                child: Switch(
                                    value: appModel.is_focus,
                                    onChanged: (value) {
                                      if (appModel.is_focus == true) {
                                        appModel.is_focus = false;
                                        controller
                                            .requestDataWithRemove(appModel.id);
                                      } else {
                                        appModel.is_focus = true;
                                        controller
                                            .requestDataWithFocus(appModel.id);
                                        // controller.requestData().then((value) {
                                        //   appModel.is_focus = true;
                                        //   print('value ========= ${value}');
                                        //
                                        // });
                                      }
                                    }),
                              )
                            ],
                          ),
                          Container(
                            height: 0.5,
                            color: AppColor.lineColor,
                          )
                        ],
                      ),
                    );
                  },
                  itemCount: controller.model?.data?[index].apps?.length ?? 0,
                  onReorder: (int oldIndex, int newIndex) {
                    controller.listMove(oldIndex, newIndex, index);
                    // setState(() {
                    //   print("$oldIndex to $newIndex");
                    //   if (oldIndex < newIndex) {
                    //     //向下移动的情况,需要减去1，不然会index 溢出
                    //     newIndex -= 1;
                    //   }
                    //   // final int item = itemList.removeAt(oldIndex);
                    //   // itemList.insert(newIndex, item);
                    //
                    //   Apps reapp = _model!.data![index].apps!.removeAt(oldIndex);
                    //   _model!.data![index].apps!.insert(newIndex, reapp);
                    //
                    //   // Car redata = datas.removeAt(oldindex);
                    //   // datas.insert(newindex, redata);
                    // });
                  })
            ],
          ),
        );
      },
      childCount: controller.dataArr.length,
    );
  }
}
